﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using FisheryPlatform.Search.Core.Utils;

namespace FisheryPlatform.Search.Core.Resource
{
    /// <summary>
    /// An implementation of <see cref="Castle.Core.Resource.IResourceFactory"/>
    /// to handle the "path://..." style external conf path.
    /// </summary>
    /// <author>Xuliye</author>
    /// <version>$1.0|2008-7-24$</version>
    internal class CentralPathResourceFactory : IResourceFactory
    {
        public static readonly string BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
        private static readonly string Scheme = "path";

        #region IResourceFactory Members
        /// <summary>
        /// 接受
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public bool Accept(CustomUri uri)
        {
            return Scheme.Equals(uri.Scheme);
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="basePath"></param>
        /// <returns></returns>
        public IResource Create(CustomUri uri, string basePath)
        {
            return Create(uri);
        }
        /// <summary>
        /// 创建定制的路径
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public IResource Create(CustomUri uri)
        {
            var fileUri = RebuildUri(uri);
            var basePath = GetBasePath();

            string file = Path.GetFileName(fileUri.Path);
            if (file.Length != 0)
                return new CentralPathFileResource(fileUri, basePath);
            return new CentralPathDirectoryResource(Path.Combine(basePath, fileUri.Path));
        }
        /// <summary>
        /// 得到基础路径
        /// </summary>
        /// <returns></returns>
        public static string GetBasePath()
        {
            var path = AppSettings.Get(Scheme, BaseDirectory);
            path = path.Replace("${solution.dir}", Path.Combine(BaseDirectory, "..\\..\\.."));
            path = path.Replace("${project.dir}", Path.Combine(BaseDirectory, "..\\.."));
            path = path.Replace("${web.solution.dir}", Path.Combine(BaseDirectory, ".."));
            path = path.Replace("${bin.dir}", BaseDirectory);
            path = path.Replace("${current.dir}", ".");

            return path;
        }

        private static CustomUri RebuildUri(CustomUri uri)
        {
            string identifier = string.Format("{0}{1}{2}{3}",
                                              CustomUri.UriSchemeFile,
                                              CustomUri.SchemeDelimiter,
                                              uri.Host,
                                              uri.Path);

            foreach (string key in CentralPath.PathVar.Keys)
            {
                identifier = identifier.Replace(key, CentralPath.PathVar[key]);
            }

            return new CustomUri(identifier);
        }

        #endregion
    }
}
